package cn.jh.clearing.business.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import cn.jh.clearing.business.DistributionBusiness;
import cn.jh.clearing.pojo.DistributionRecord;
import cn.jh.clearing.repository.DistributionRecordRepository;

@Service
public class DistributionBusinessImpl implements DistributionBusiness{
	@Autowired
	DistributionRecordRepository drr;

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Page<DistributionRecord> findDistributionRecordByordercode(String order, Date strTime, Date endTime,
			Pageable page) {
		// TODO Auto-generated method stub
		return drr.findDistributionRecordByordercode(order, strTime, endTime, page);
	}

	@Override
	public Page<DistributionRecord> findAllDistributionByPhone(String acqphone, Date strTime, Date endTime,
			Pageable page) {
		// TODO Auto-generated method stub
		return drr.findAllDistributionByPhone(acqphone, strTime, endTime, page);
	}

	@Override
	public Page<DistributionRecord> findAllDistributionByoriPhone(String oriphone,String[] order, Date strTime, Date endTime,
			Pageable page) {
		// TODO Auto-generated method stub
		return drr.findAllDistributionByoriPhone(oriphone,order, strTime, endTime, page);
	}

	@Override
	public Page<DistributionRecord> findAllDistributionByPhoneAndOrder(String oriphone, String order, Date strTime,
			Date endTime, Pageable page) {
		// TODO Auto-generated method stub
		return drr.findAllDistributionByPhoneAndOrder(oriphone, order, strTime, endTime, page);
	}

	@Override
	public Page<DistributionRecord> findAllDistributionByPhoneAndacqOrder(String acqphone, String order, Date strTime,
			Date endTime, Pageable page) {
		// TODO Auto-generated method stub
		return drr.findAllDistributionByPhoneAndacqOrder(acqphone, order, strTime, endTime, page);
	}

	@Override
	public Page<DistributionRecord> findAllDistributionByPhoneAndoriOrder(String acqphone, String oriphone,
			Date strTime, Date endTime, Pageable page) {
		// TODO Auto-generated method stub
		return drr.findAllDistributionByPhoneAndoriOrder(acqphone, oriphone, strTime, endTime, page);
	}

	@Override
	public Page<DistributionRecord> findByAllParams(String acqphone, String oriphone, String order, Date strTime,
			Date endTime, Pageable page) {
		// TODO Auto-generated method stub
		return drr.findByAllParams(acqphone, oriphone, order, strTime, endTime, page);
	}

	@Override
	public BigDecimal queryDistributionSumAcqAmountByPhone(String phone, String startTime, String endTime) {

		StringBuffer sql = new StringBuffer("select sum(acq_amount) from t_distribution_record where acq_phone=" + phone);
		
		if (startTime != null && !"".equals(startTime)) {
			sql.append(" and date_format(create_time,'%Y-%m-%d')>='" + startTime + "'");
		}
		if (endTime != null && !"".equals(endTime)) {
			sql.append(" and date_format(create_time,'%Y-%m-%d')<='" + endTime + "'");
		}
		
		Map<String, Object> distributionSumAmount = jdbcTemplate.queryForMap(sql.toString());
		BigDecimal big = null;
		if (distributionSumAmount.get("sum(acq_amount)") != null) {
			big = (BigDecimal) distributionSumAmount.get("sum(acq_amount)");
		} else {
			double d = 0.00;
			big = big.valueOf(d);
		}
		
		return big;
		
	}
}
